<template>
  <div class="flexContent">
    <!-- 收货管理 -->
    <div class="searchBox">

      <el-form ref="form" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="收货单号">
                  <el-input v-clearEmoji v-model.trim="sform.number" style="width: 95%" clearable placeholder="请输入收货单号"
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>

              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="状态">
                  <el-select v-model="sform.status" clearable popper-class="eloption" placeholder="请选择状态"
                    style="width: 95%">
                    <el-option v-for="(val, key) in statusList" :key="key" :label="val" :value="key" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="供应商名称">
                  <el-select v-model="sform.supplierId" clearable filterable popper-class="eloption"
                    placeholder="请选择供应商名称" style="width: 95%" @click.native="getSCMList">
                    <el-option v-for="item in firstSupplierList" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6" v-if="isAdmin">
                <el-form-item label="收货员">
                  <el-select v-model="sform.userId" clearable filterable popper-class="eloption" placeholder="请选择收货员"
                    style="width: 95%" @click.native="reqBuyerList">
                    <el-option v-for="item in userList" :key="item.id" :label="item.employeeName" :value="item.id" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="收货日期">
                  <el-date-picker v-model="date" clearable type="daterange" unlink-panels range-separator="-"
                    start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" style="width: 95%" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetForm" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" v-has="'receiving_add'" @click="toAddPage">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" align="center" style="width: 100%"
          height="100%" size="medium">
          <vxe-column type="seq" title="序号" width="80" />
          <vxe-column field="number" title="收货单号" :min-width="elTableAdaptationWidth('number', tableData)" />
          <vxe-column field="warehouseName" title="仓库" :min-width="elTableAdaptationWidth('warehouseName', tableData)" />
          <vxe-column field="supplierName" title="供应商名称" :min-width="elTableAdaptationWidth('supplierName', tableData)" />
          <vxe-column field="username" title="收货员" min-width="150" />
          <vxe-column field="takeTime" title="收货日期" min-width="150">
            <template v-slot="{ row }">
              <span>{{ row.takeTime | parseTime }}</span>
            </template>
          </vxe-column>
          <vxe-column field="status" title="状态" width="100">
            <template v-slot="{ row }">
              <el-tag v-if="row.status === '0'" disabled type="info" size="small">待入库</el-tag>
              <el-tag v-if="row.status === '1'" disabled type="primary" size="small">已入库</el-tag>
            </template>
          </vxe-column>
          <vxe-column fixed="right" title="操作" width="100" v-if="$showFn(['receiving_detail', 'receiving_print'])">
            <template slot-scope="scope">
              <el-button type="text" @click="toDetail(scope.row.id)" v-has="'receiving_detail'">详情</el-button>
              <el-button type="text" @click="getOrderDetailById(scope.row.id)" v-has="'receiving_print'">打印</el-button>
              <!-- <el-button type="text" style="color: red;" @click="delRegionById(scope.row.id)">删除</el-button> -->
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getDictValueAndNameSource } from "@/api/dictionary";
import { getSCMList } from "@/api/scm";
import { isAdmin } from "@/api/user";
import { reqBuyerList } from "@/api/procure";
import { getReceivingOrderList, getReceivingOrderDetail } from "@/api/warehouse";
import { printReceiving, checkPrinter } from '@/utils/printLODOP';
export default {
  name: "Receiving",
  data() {
    return {
      isSearch: false,
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      date: [],
      sform: {
        number: "",
        supplierId: "",
        userId: "",
        status: "",
        maxDate: "",
        minDate: "",
      },
      tableData: [],
      firstSupplierList: [],
      userList: [],
      statusList: {},
      isAdmin: false,
    };
  },
  created() {
    // this.getSCMList();
    // this.reqBuyerList();
    this.getStatusList();
    this.getReceivingOrderList();
    this.getIsAdmin();
  },
  methods: {
    async getIsAdmin() {
      const res = await isAdmin();
      if (res.code === 200) {
        console.log(res, "成功");
        this.isAdmin = res.data;
      }
      console.log(res);
    },
    async getStatusList() {
      const res = await getDictValueAndNameSource("TAKE_DELIVERY_STATUS");
      if (res.code === 200) {
        this.statusList = res.data;
        console.log(this.statusList, "status");
      } else {
        console.log(res.data);
      }
    },
    async getSCMList() {
      const res = await getSCMList();
      if (res.code === 200) {
        const data = res.data;
        this.firstSupplierList = data.list;
      }
    },
    async reqBuyerList() {
      const res = await reqBuyerList({ code: "1" });
      if (res.code === 200) {
        const data = res.data;
        this.userList = data;
        console.log(res.data);
      }
    },
    async getReceivingOrderList() {
      if (!this.isSearch) {
        this.sform = {
          number: "",
          supplierId: "",
          userId: "",
          status: "",
          maxDate: "",
          minDate: "",
        };
      }
      const res = await getReceivingOrderList({ ...this.page, ...this.sform });
      if (res.code === 200) {
        this.tableData = res.data.list;
        this.page.pageSize = res.data.pageSize;
        this.page.pageNum = res.data.pageNum;
        this.page.total = res.data.total;
        console.log(res.data, "收货单");
        const $table = this.$refs.xTable1
        $table.loadData(this.tableData);
      }
    },
    resetForm(formName) {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.sform = {
        number: "",
        supplierId: "",
        userId: "",
        status: "",
        maxDate: "",
        minDate: "",
      };
      this.isSearch = false;
      this.date = [];
      this.getReceivingOrderList();
    },
    toAddPage() {
      this.$router.push({
        path: "receivingAdd",
      });
    },
    toDetail(id) {
      console.log(id);
      this.$router.push({
        path: "receivingDetail",
        query: {
          id: id,
        },
      });
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getReceivingOrderList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getReceivingOrderList();
    },
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = true;
      this.sform.maxDate = this.date?.length ? this.date[1] : "";
      this.sform.minDate = this.date?.length ? this.date[0] : "";
      this.getReceivingOrderList();
    },
    getOrderDetailById(id) {
      this.$confirm(`即将打印收货单，是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        if (!checkPrinter()) return;
        const res = await getReceivingOrderDetail({ id });
        if (res.code === 200) {
          this.printObj = res.data;
          console.log(res.data);
          printReceiving(this.printObj);
          this.$message({ type: "success", message: "正在打印收货单" });
        }
      });

    },
  },
};
</script>

<style lang="scss" scoped></style>
